package org.example.Thread02;

import java.util.concurrent.locks.ReentrantLock;

public class Dome_Thread04 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter();
        Thread t1 = new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });
        t1.start();
        Thread t2 = new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.count);
    }
}
class Counter{
    public int count=0;
    //初始化一把锁
    ReentrantLock lock=new ReentrantLock();
    public void increase(){
        try {
            //上锁
            lock.lock();
            count++;
        } finally {
            //释放锁
            lock.unlock();
        }
    }
}
